package com.itqf.service.Impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itqf.entity.SysUser;
import com.itqf.entity.SysUserExample;
import com.itqf.mapper.SysUserMapper;
import com.itqf.service.SysUsersService;
import com.itqf.utils.*;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * @author 徐老板
 * @date 2020/1/25   13:00
 */
@Service
public class SysUsersServiceImpl implements SysUsersService {

    @Resource
    private SysUserMapper sysUserMapper;

    @Override
    public List<SysUser> findAll() {
        return sysUserMapper.selectByExample(null);
    }

    @Override
    public R login(SysUser sysUser) {
        SysUserExample sysUserExample = new SysUserExample();
        SysUserExample.Criteria criteria = sysUserExample.createCriteria();
        criteria.andUsernameEqualTo(sysUser.getUsername());
        List<SysUser> sysUsers = sysUserMapper.selectByExample(sysUserExample);
        if (sysUsers==null||sysUsers.size()==0){
            return R.error("账户为空");
        }
        //在数组中键为数字 值为信息
//        SysUser sysUserDB=sysUsers[0];
        SysUser sysUserDB=sysUsers.get(0);
        if (!sysUserDB.getPassword().equals(sysUser.getPassword())){
            return R.error("密码错误");
        }
        //判断条件错误 0表示账号被锁定 1 表示账号没事
//        if (!sysUserDB.getStatus().equals(sysUser.getStatus())){
//            return R.error("账户被锁定")；
//        }
        if (sysUserDB.getStatus()==0){
            return R.error("账户被锁定");

        }

        return R.ok().put("u", sysUserDB);
    }

    //使用shiro执行登陆操作
    @Override
    public List<SysUser> findByName(String username) {
        SysUserExample sysUserExample=new SysUserExample();
        SysUserExample.Criteria criteria = sysUserExample.createCriteria();
        criteria.andUsernameEqualTo(username);
        List<SysUser> sysUserList = sysUserMapper.selectByExample(sysUserExample);

        if (sysUserList!=null&&sysUserList.size()>0){

            return sysUserList;
        }else {
            return null;
        }

    }

    @Override
    public ResultData userByPage(Pager pager, String search, Sorter sorter) {
        PageHelper.offsetPage(pager.getOffset(),pager.getLimit() );
        SysUserExample sysUserExample=new SysUserExample();
        SysUserExample.Criteria criteria = sysUserExample.createCriteria();

        if (search!=null&&"".equals(search)){
            criteria.andUsernameLike("%"+search+"%");
        }
        if (sorter!=null&& StringUtils.notEmpty(sorter.getSort())){
            sysUserExample.setOrderByClause("user_id "+sorter.getOrder());
        }

        List<SysUser> sysUserList = sysUserMapper.selectByExample(sysUserExample);
        PageInfo pageInfo = new PageInfo(sysUserList);
        long total = pageInfo.getTotal();
        List list = pageInfo.getList();

        return new ResultData(total,list );
    }

    @Override
    public List<String> findRoleByUID(long userId) {
        List<String> roleList = sysUserMapper.findRoleByUID(userId);
        HashSet<String> set = new HashSet<String>();
        List<String> list = new ArrayList<>();

        if (roleList != null && roleList.size() > 0) {
            set.addAll(roleList);
            list.addAll(set);
            return list;
        } else {
            return null;

        }
    }

    @Override
    public List<String> findPermsByUID(long userId) {
        List<String> permsList = sysUserMapper.findPermsByUID(userId);
        HashSet<String> set = new HashSet<String>();
        List<String> list = new ArrayList<>();

        if (permsList != null && permsList.size() > 0) {
            for (String s : permsList) {
                if (StringUtils.notEmpty(s)) {
                    String[] arr = s.split(",");
                    for (int i = 0; i <arr.length; i++) {
                        set.add(arr[i]);
                    }
                }
            }
            list.addAll(set);
            return list;
        } else {
            return null;

        }

    }

    @Override
    public R findBarData() {
        List<Map<String, Object>> barDataList = sysUserMapper.findBarData();
        List<String> deptList=new ArrayList<>();
        List manList=new ArrayList<>();
        List woemnList=new ArrayList<>();

        for (Map<String, Object> map : barDataList) {
            String deptName = map.get("deptName")+"";
            deptList.add(deptName);
            Object man = map.get("man");
            manList.add(man);
            Object women = map.get("women");
            woemnList.add(women);
        }

        return R.ok().put("deptList",deptList ).put("manList", manList).put("womenList", woemnList);
    }

    @Override
    public R findPieData() {
        List<Map<String, Object>> pieData = sysUserMapper.findPieData();
        List<String> deptNameList=new ArrayList<>();

        for (Map<String, Object> pieDatum : pieData) {
            String deptName = pieDatum.get("deptName")+"";
            deptNameList.add(deptName);
        }

        return R.ok().put("deptName", deptNameList).put("pieData", pieData);
    }

    @Override
    public List<Map<String, Object>> exportPoi() {
        List<Map<String, Object>> poiList = sysUserMapper.poiList();

        return poiList;
    }


}
